package com.ruoyi.datam.controller;

import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.domain.base.datam.WiMainB;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.datam.service.IWiMainBService;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 水管员信息登记Controller
 * 
 * @author hwd
 * @date 2023-02-16
 */
@RestController
@RequestMapping("/WiMainB")
public class WiMainBController extends BaseController
{
    @Autowired
    private IWiMainBService wiMainBService;

    /**
     * 根据水管员名称查找水管员编码
     */
    @GetMapping("/geMainCd")
    public AjaxResult getFarmCd(String mainNm){
        String FarmCd = wiMainBService.geMainCd(mainNm);
        HashMap<String, String> res = new HashMap<>();
        res.put("mainCd",FarmCd);
        return AjaxResult.success(res);
    }

    /**
     * 查询水管员信息登记列表
     */
    @RequiresPermissions("datam:WiMainB:list")
    @GetMapping("/list")
    public TableDataInfo list(WiMainB wiMainB)
    {
        startPage();
        List<WiMainB> list = wiMainBService.selectWiMainBList(wiMainB);
        return getDataTable(list);
    }

    /**
     * 导出水管员信息登记列表
     */
    @RequiresPermissions("datam:WiMainB:export")
    @Log(title = "水管员信息登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WiMainB wiMainB)
    {
        List<WiMainB> list = wiMainBService.selectWiMainBList(wiMainB);
        ExcelUtil<WiMainB> util = new ExcelUtil<WiMainB>(WiMainB.class);
        util.exportExcel(response, list, "水管员信息登记数据");
    }

    /**
     * 获取水管员信息登记详细信息
     */
    @RequiresPermissions("datam:WiMainB:query")
    @GetMapping(value = "/{mainCd}")
    public AjaxResult getInfo(@PathVariable("mainCd") String mainCd)
    {
        return success(wiMainBService.selectWiMainBByMainCd(mainCd));
    }

    /**
     * 新增水管员信息登记
     */
    @Transactional(rollbackFor = Exception.class)
    @RequiresPermissions("datam:WiMainB:add")
    @Log(title = "水管员信息登记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WiMainB wiMainB)
    {
        String s = wiMainBService.geMainCd(wiMainB.getMainNm());
        if(StringUtils.isNotBlank(s)){
            AjaxResult.error("该用户名称已存在~");
        }
        if(StringUtils.isNotBlank(wiMainB.getMobileNo())){
            AjaxResult.error("请认真填写手机号~");
        }
        int i = wiMainBService.insertWiMainB(wiMainB);
        if (i > 0) {//向user中添加數據 角色為水管員
            SysUser sysUser = new SysUser();
            sysUser.setUserName(wiMainB.getMainNm());
            sysUser.setPhonenumber(wiMainB.getMobileNo());
            sysUser.setNickName(wiMainB.getMainNm());
            sysUser.setRoleId(3L);
            sysUser.setDeptId(100L);
            if (UserConstants.NOT_UNIQUE.equals(wiMainBService.checkUserNameUnique(sysUser)))
            {
                return error("新增用户'" + sysUser.getUserName() + "'失败，登录账号已存在");
            }
            else if (StringUtils.isNotEmpty(sysUser.getPhonenumber())
                    && UserConstants.NOT_UNIQUE.equals(wiMainBService.checkPhoneUnique(sysUser)))
            {
                return error("新增用户'" + sysUser.getUserName() + "'失败，手机号码已存在");
            }
            sysUser.setCreateBy(SecurityUtils.getUsername());
            sysUser.setPassword(SecurityUtils.encryptPassword(wiMainB.getMobileNo()+"@fuyun"));
            wiMainBService.insertUser(sysUser);
        }
        return toAjax(i);
    }

    /**
     * 修改水管员信息登记
     */
    @RequiresPermissions("datam:WiMainB:edit")
    @Log(title = "水管员信息登记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WiMainB wiMainB)
    {
        return toAjax(wiMainBService.updateWiMainB(wiMainB));
    }

    /**
     * 删除水管员信息登记
     */
    @RequiresPermissions("datam:WiMainB:remove")
    @Log(title = "水管员信息登记", businessType = BusinessType.DELETE)
	@DeleteMapping("/{mainCds}")
    public AjaxResult remove(@PathVariable String[] mainCds)
    {
        return toAjax(wiMainBService.deleteWiMainBByMainCds(mainCds));
    }
}
